//
//  GetUserAccountInfoEngine.m
//  HuLaQuan
//
//  Created by hok on 1/27/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "GetUserAccountInfoEngine.h"

#import "CurrentUser.h"

@implementation GetUserAccountInfoEngine

- (void)getAccoutInfo
{
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getAccInfo\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID];
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CUSTOMER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            if ([item isKindOfClass:[NSArray class]]) {
                NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
                NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
                
                
                NSArray *arr = [self parseAttach:resultDic[@"attach"]];
                
                
                NSMutableDictionary *accountInfo = [[NSMutableDictionary alloc] init];
                [accountInfo setObject:@{[arr firstObject]:[arr lastObject]} forKey:kInitPayStatusKey];
        
                /**
                 *  判断如果有银行卡列表，则包装成数组，并存储在返回的accountInfo中，key为：cardList
                 */
                if (resultDic[@"list"] && resultDic[@"list"][@"card"]){
                    NSLog(@"%@",resultDic[@"list"][@"card"]);
                     if( [resultDic[@"list"][@"card"] isKindOfClass:[NSArray class]]) {
                        [accountInfo setObject:resultDic[@"list"][@"card"] forKey:@"cardList"];
                    }else{
                        [accountInfo setObject:@[resultDic[@"list"][@"card"]] forKey:@"cardList"];
                    }
                }
                
                
                result = @{@"isSuccess":@YES,@"result":accountInfo};
                
           
            }
        }else{
            
            result = @{@"isSuccess":@NO,@"message":item};
            
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetUserAccountInfoFinish" object:result];
    }];
}

- (NSArray *)parseAttach:(NSString *)attach
{
    NSString *tempString  = [attach stringByReplacingOccurrencesOfString:@"{" withString:@""];
    NSString *tempString2 = [tempString stringByReplacingOccurrencesOfString:@"}" withString:@""];
    NSArray *arr = [tempString2 componentsSeparatedByString:@":"];
    return arr;
}
@end
